<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="70px">
      <el-form-item label="单位编码" prop="companyCode">
        <el-input
          v-model="queryParams.companyCode"
          placeholder="请输入单位编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入单位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="社会信用代码" prop="socialCode" label-width="100px">
        <el-input
          v-model="queryParams.socialCode"
          placeholder="请输入社会信用代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="contacts">
        <el-input
          v-model="queryParams.contacts"
          placeholder="请输入联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="telephone">
        <el-input
          v-model="queryParams.telephone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="editRowCompany"
          v-hasPermi="['physical:basecompany:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['physical:basecompany:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleImport"
          v-hasPermi="['physical:basecompany:import']"
        >导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="basecompanyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="单位编码" width="150" align="center" prop="companyCode" />
      <el-table-column label="单位名称" width="200" align="center" prop="companyName" />
      <el-table-column label="社会信用代码" align="center" prop="socialCode" />
      <el-table-column label="联系人" align="center" prop="contacts" />
      <el-table-column label="联系电话" align="center" prop="telephone" />
      <el-table-column label="地区" align="center" prop="area">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dime_physical_area" :value="scope.row.area"/>
        </template>
      </el-table-column>
      <el-table-column label="行业类别" align="center" prop="industry">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dime_physical_industry" :value="scope.row.industry"/>
        </template>
      </el-table-column>
      <el-table-column label="经济类型" align="center" prop="economy">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dime_physical_economy" :value="scope.row.economy"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="editRowCompany(scope.row)"
            v-hasPermi="['physical:basecompany:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['physical:basecompany:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改体检单位管理对话框 -->
    <el-dialog title="编辑单位信息" :visible.sync="openCompany" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="单位编码" prop="companyCode">
              <el-input v-model="form.companyCode" placeholder="请输入单位编码" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位名称" prop="companyName">
              <el-input v-model="form.companyName" placeholder="请输入内容" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="上级单位" prop="superiorCompany">
              <el-input v-model="form.superiorCompany" placeholder="请输入上级单位"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用工单位" prop="employingCompany">
              <el-input v-model="form.employingCompany" placeholder="请输入用工单位"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="社会信用代码" prop="socialCode">
              <el-input v-model="form.socialCode" placeholder="请输入社会信用代码"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮编" prop="postalCode">
              <el-input v-model="form.postalCode" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="联系人" prop="contacts">
              <el-input v-model="form.contacts" placeholder="请输入联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="telephone">
              <el-input v-model="form.telephone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="地区" prop="area">
              <el-select v-model="form.area" filterable clearable placeholder="请选择地区" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.dime_physical_area"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位地址" prop="address">
              <el-input v-model="form.address" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="行业类别" prop="industry">
              <el-select v-model="form.industry" filterable clearable placeholder="请选择行业类别" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.dime_physical_industry"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经济类型" prop="economy">
              <el-select v-model="form.economy" filterable clearable placeholder="请选择经济类型" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.dime_physical_economy"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="企业规模" prop="scale">
              <el-select v-model="form.scale" filterable clearable placeholder="请选择企业规模" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.dime_physical_scale"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="职工数" prop="workersNumber">
              <el-input v-model="form.workersNumber" @input="(v)=>(form.workersNumber=v.replace(/[^\d]/g,''))" maxlength="8" placeholder="请输入职工数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="男职工" prop="maleWorkers">
              <el-input v-model="form.maleWorkers" @input="(v)=>(form.maleWorkers=v.replace(/[^\d]/g,''))" maxlength="8" placeholder="请输入男职工" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="女职工" prop="femaleWorkers">
              <el-input v-model="form.femaleWorkers" @input="(v)=>(form.femaleWorkers=v.replace(/[^\d]/g,''))" maxlength="8" placeholder="请输入女职工" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="生产人数" prop="producerNumber">
              <el-input v-model="form.producerNumber" @input="(v)=>(form.producerNumber=v.replace(/[^\d]/g,''))" maxlength="8" placeholder="请输入生产人数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产女工数" prop="femaleProducer">
              <el-input v-model="form.femaleProducer" @input="(v)=>(form.femaleProducer=v.replace(/[^\d]/g,''))" maxlength="8" placeholder="请输入生产女工数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="接害人数" prop="harmNumber">
              <el-input v-model="form.harmNumber" @input="(v)=>(form.harmNumber=v.replace(/[^\d]/g,''))" maxlength="8" placeholder="请输入接害人数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="接害女工数" prop="femaleHarm">
              <el-input v-model="form.femaleHarm" @input="(v)=>(form.femaleHarm=v.replace(/[^\d]/g,''))" maxlength="8" placeholder="请输入接害女工数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="检测类型" prop="detectionType">
              <el-select v-model="form.detectionType" placeholder="请选择检测类型" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.dime_physical_detection"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">

          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitCompanyForm">确 定</el-button>
        <el-button @click="cancelCompany">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 导入套餐对话框 -->
    <el-dialog title="编辑套餐信息" :visible.sync="openPackage" width="800px" append-to-body>
      <el-form ref="packageForm" :model="packageForm" :rules="rules" label-width="100px">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="套餐编码"  prop="packageCode">
              <el-input v-model="packageForm.packageCode" placeholder="请输入套餐编码" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="套餐名称" prop="packageName">
              <el-input v-model="packageForm.packageName" placeholder="请输入套餐名称" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="在岗状态" prop="worktype">
              <el-select v-model="packageForm.worktype" placeholder="请选择在岗状态" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.dime_physical_work"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="体检类型" prop="checkTypeEnum">
              <el-select v-model="packageForm.checkTypeEnum" placeholder="请选择体检类型" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.dime_physical_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="价格" prop="packageOriginalPrice">
              <el-input-number v-model="packageForm.packageOriginalPrice" :min="0" label="请输入原价" style="width: 100%"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="折扣价" prop="packageDiscountPrice">
              <el-input-number v-model="packageForm.packageDiscountPrice" :min="0" label="请输入协议价" style="width: 100%"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="适用性别" prop="sexEnum">
              <el-select v-model="packageForm.sexEnum" placeholder="请选择适用性别" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.sys_user_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="婚否" prop="marriageTag">
              <el-select v-model="packageForm.marriageTag" placeholder="请选择婚否" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.dime_physical_marrie"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="有效期始" prop="startDate">
              <el-date-picker clearable
                              style="width: 100%"
                              v-model="packageForm.startDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择有效期始">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="有效期止" prop="endDate">
              <el-date-picker clearable
                              style="width: 100%"
                              v-model="packageForm.endDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择有效期止">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row >
          <el-col :span="24">
            <el-form-item label="危害因素" prop="dimeBaseharmList">
              <el-tree-select
                ref="treeSelect"
                v-model="packageForm.dimeBaseharmList"
                popover-class="test-class-wrap"
                value-key="id"
                :select-params="selectParams"
                :tree-params="treeParams"
                :filter-node-method="_filterFun"
                @searchFun="_searchFun"
                @removeTag="harmfactorChangeRemove"
                @check="harmfactorChange"
                @select-clear="clearHarm"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="接害因素" prop="touchHarms">
              <el-input v-model="packageForm.touchHarms" type="text" placeholder="请输入接触危害" clearable />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitPackageForm">确 定</el-button>
        <el-button @click="cancelPackage">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 导入弹框 -->
    <el-dialog title="导入体检单位" :visible.sync="openImport" width="1000px" append-to-body :fullscreen="true">
      <el-form label-width="100px">
        <el-form-item label="SOA订单编码" prop="SOACode" v-if="showImport">
          <div style="display: flex">
            <el-input v-model="SOACode" placeholder="请输入SOA订单编码"style="width: 400px"/>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="search" style="margin-left: 10px">导入</el-button>
          </div>
        </el-form-item>
        <el-table v-loading="importLoading" :data="companyList" :default-expand-all="true">
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-table :data="basepackageList" @selection-change="handleSelectionChange">
                <el-table-column type="expand">
                  <template slot-scope="props">
                    <el-table :data="props.row.physicalCombDtoList" @selection-change="handleSelectionChange">
                      <el-table-column label="组合编码" align="center" prop="combCode" />
                      <el-table-column label="组合名称" align="center" prop="combName" />
                      <el-table-column label="组合类型" align="center" prop="combType">
                        <template slot-scope="scope">
                          <dict-tag :options="dict.type.dime_comb_type" :value="scope.row.combType"/>
                        </template>
                      </el-table-column>
                      <el-table-column label="适用性别" align="center" prop="applySex">
                        <template slot-scope="scope">
                          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.applySex"/>
                        </template>
                      </el-table-column>
                      <el-table-column label="组合价格" align="center" prop="combPrice" />
                      <el-table-column label="描述" align="center" prop="describe" />
                      <el-table-column label="所属科室" align="center" prop="groupName" />
                    </el-table>
                  </template>
                </el-table-column>
                <el-table-column label="套餐编码" width="200" align="center" prop="packageCode" />
                <el-table-column label="套餐名称" width="200" align="center" prop="packageName" />
                <el-table-column label="原价" align="center" prop="packageOriginalPrice" />
                <el-table-column label="折扣价" align="center" prop="packageDiscountPrice" />
                <el-table-column label="体检类型" align="center" prop="checkTypeEnum">
                  <template slot-scope="scope">
                    <dict-tag :options="dict.type.dime_physical_type" :value="scope.row.checkTypeEnum"/>
                  </template>
                </el-table-column>
                <el-table-column label="在岗状态" align="center" prop="worktype">
                  <template slot-scope="scope">
                    <dict-tag :options="dict.type.dime_physical_work" :value="scope.row.worktype"/>
                  </template>
                </el-table-column>
                <el-table-column label="危害因素" width="200" align="center" prop="dimeBaseharmList">
                 <template slot-scope="scope">
                   <el-tag
                     v-for="(tag,index) in getHarmLabel(scope.row.dimeBaseharmList)"
                     :key="index"
                     >
                     {{tag.hamName}}
                   </el-tag>
                 </template>
                </el-table-column>
                <el-table-column label="有效期始" align="center" prop="startDate" width="180">
                  <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
                  </template>
                </el-table-column>
                <el-table-column label="有效期止" align="center" prop="endDate" width="180">
                  <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
                  </template>
                </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                  <template slot-scope="scope">
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="editPackage(scope)"
                    >编辑</el-button>
                  </template>
                </el-table-column>
              </el-table>
            </template>
          </el-table-column>
          <el-table-column label="单位编码" align="center" prop="companyCode" />
          <el-table-column label="单位名称" align="center" prop="companyName" />
          <el-table-column label="社会信用代码" align="center" prop="socialCode" />
          <el-table-column label="联系人" align="center" prop="contacts" />
          <el-table-column label="联系电话" align="center" prop="telephone" />
          <el-table-column label="地址" align="center" prop="address"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="editCompany(scope.row)"
              >编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBasecompany,
  delBasecompany,
  saveImportCompany,
  editSaveBQCompany,
  importCompany,
  getallharmtype,
  editSearchBQCompanyById
} from "@/api/physical/basecompany";
export default {
  name: "basecompany",
  dicts: ['dime_physical_scale', 'dime_physical_industry', 'dime_physical_economy', 'dime_physical_area',
    'dime_physical_detection','dime_physical_work','dime_physical_type','dime_physical_marrie','dime_comb_type','sys_user_sex'],
  data() {
    return {
      showImport:true,
      SOACode:null,
      companyInfo:{},
      companyList:[],
      // 遮罩层
      loading: false,
      importLoading: false,
      // 选中数组
      ids: [],
      codes:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 体检单位管理表格数据
      basecompanyList: [],
      //套餐数据
      basepackageList: [],
      // 是否显示弹出层
      openCompany: false,
      openPackage: false,
      //导入弹框
      openImport: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyCode: null,
        companyName: null,
        socialCode: null,
        address: null,
        postalCode: null,
        contacts: null,
        telephone: null,
      },
      packageForm:{
        id: null,
        dimeBaseharmList:[],
        touchHarms:null,
        packageCode: null,
        packageName: null,
        checkTypeEnum: null,
        companyNameId: null,
        packageOriginalPrice: null,
        packageDiscountPrice: null,
        sexEnum: null,
        marriageTag: null,
        startDate: null,
        endDate: null,
        examineTag: null,
        examiner: null,
        examineDate: null,
        publicTag: null,
        description: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        IsValid: null,
        shopCode: null
      },
      packageIndex: 0,
      harmtList:[],
      // 表单参数
      form: {
        id: null,
        companyCode: null,
        companyName: null,
        socialCode: null,
        address: null,
        postalCode: null,
        contacts: null,
        telephone: null,
        area: null,
        industry: null,
        economy: null,
        scale: null,
        workersNumber: null,
        maleWorkers: null,
        femaleWorkers: null,
        producerNumber: null,
        femaleProducer: null,
        harmNumber: null,
        femaleHarm: null,
        detectionType: null,
        createTime: null,
        createBy: null,
        updateBy: null,
        updateTime: null,
        IsValid: null,
        shopCode: null,
        superiorCompany: null,
        employingCompany: null
      },
      // 表单校验
      rules: {
        id: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        companyName: [
          { required: true, message: "单位名称不能为空", trigger: "blur" }
        ],
      },
      // tree select
      selectParams: {
        clearable: true,
        placeholder: '请输入内容',
        multiple: true
      },
      treeParams: {
        clickParent: true,
        filterable: true,
        // 只想要子节点，不需要父节点
        leafOnly: true,
        showCheckbox: true,
        includeHalfChecked: false,
        'check-strictly': false,
        'default-expand-all': true,
        'expand-on-click-node': false,

        data: [],
        props: {
          children: 'harms',
          label: 'hamName',
          rootId: '0',
          disabled: 'disabled',
          parentId: 'pid',
          value: 'id'
        }
      },
    };
  },
  created() {
    this.getList();
    getallharmtype().then(response => {
      this.harmtList = response.data;
      this.harmtList.forEach(harm=>{
        harm.id = harm.harmType
        harm.hamName = harm.harmTypeName
      })
      this.treeParams.data = this.harmtList
    });
  },
  methods: {
    getHarmLabel(datas) {
      if(!datas) return []
      let harms = []
      this.harmtList.forEach(item=>{
        if(item.harms) {
          harms.push(...item.harms)
        }
      })
      let labels = datas.map(item=>{
        for(let i = 0; i < harms.length; i++) {
          if(item == harms[i].id) {
            return harms[i]
          }
        }
      })
      return labels
    },
    harmfactorChangeRemove(data, node) {
      this.packageForm.touchHarms = this.getHarmLabel(data).map((t) => { return t.hamName }).join(',')
    },
    clearHarm(){
      this.packageForm.touchHarms=null
    },
    harmfactorChange(data, node) {
      this.packageForm.touchHarms = this.getHarmLabel(node.checkedKeys).map((t) => { return t.hamName }).join(',')
    },
    // Harmfactortree
    _filterFun(value, data, node) {
      if (!value) return true
      return data.hamName.indexOf(value) !== -1
    },
    // 树过滤
    _searchFun(value) {
      // 自行判断 是走后台查询，还是前端过滤
      this.$refs.treeSelect.filterFun(value)
      // 后台查询
      // this.$refs.treeSelect.treeDataUpdateFun(treeData);
    },
    search(){
      //this.SOACode = 'SOA36892923234110249'
      if(this.SOACode==null){
        this.$message.warning(`请输入SOA编码`)
        return
      }
      this.importLoading = true;
      importCompany(this.SOACode).then(response => {
        this.companyInfo = response.data;
        this.companyList=[this.companyInfo]
        if(this.companyInfo.basepackageList) {
          this.companyInfo.basepackageList.forEach(item=>{
            if(item.dimeBaseharmList) {
              item.dimeBaseharmList = item.dimeBaseharmList.map(harm=>{
                return harm.id
              })
            }
          })
        }
        this.basepackageList=this.companyInfo.basepackageList
      }).finally(()=>{
        this.importLoading = false;
      });
    },
    /** 查询体检单位管理列表 */
    getList() {
      this.loading = true;
      listBasecompany(this.queryParams).then(response => {
        this.basecompanyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.openImport = false;
      this.reset();
    },
    cancelPackage(){
     this.openPackage=false;
     this.resetpackageForm()
    },
    cancelCompany(){
      this.openCompany=false;
      this.reset()
    },
    resetpackageForm() {
      this.packageForm = {
        id: null,
        packageCode: null,
        packageName: null,
        checkTypeEnum: null,
        companyNameId: null,
        packageOriginalPrice: null,
        packageDiscountPrice: null,
        sexEnum: null,
        marriageTag: null,
        startDate: null,
        endDate: null,
        examineTag: null,
        examiner: null,
        examineDate: null,
        publicTag: null,
        description: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        IsValid: null,
        shopCode: null
      };
      this.resetForm("packageForm");
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        companyCode: null,
        companyName: null,
        socialCode: null,
        address: null,
        postalCode: null,
        contacts: null,
        telephone: null,
        area: null,
        industry: null,
        economy: null,
        scale: null,
        workersNumber: null,
        maleWorkers: null,
        femaleWorkers: null,
        producerNumber: null,
        femaleProducer: null,
        harmNumber: null,
        femaleHarm: null,
        detectionType: null,
        createTime: null,
        createBy: null,
        updateBy: null,
        updateTime: null,
        IsValid: null,
        shopCode: null,
        superiorCompany: null,
        employingCompany: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.codes=selection.map(item => item.companyCode)
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    editCompany(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.setDefaultZero(this.form, 'workersNumber')
      this.setDefaultZero(this.form, 'maleWorkers')
      this.setDefaultZero(this.form, 'femaleWorkers')
      this.setDefaultZero(this.form, 'producerNumber')
      this.setDefaultZero(this.form, 'femaleProducer')
      this.setDefaultZero(this.form, 'harmNumber')
      this.setDefaultZero(this.form, 'femaleHarm')
      this.openCompany = true;
    },
    editRowCompany(row){
      this.showImport=false
      this.openImport = true;
      this.importLoading = true;
      const id = row.id || this.ids
      editSearchBQCompanyById(id).then(response => {
        this.companyInfo = response.data;
        this.companyList=[this.companyInfo]
        if(this.companyInfo.basepackageList) {
          this.companyInfo.basepackageList.forEach(item=>{
            if(item.dimeBaseharmList) {
              item.dimeBaseharmList = item.dimeBaseharmList.map(harm=>{
                return harm.id
              })
            }
          })
        }
        this.basepackageList=this.companyInfo.basepackageList
      }).finally(()=>{
        this.importLoading = false;
      });
    },
    setDefaultZero(form, field) {
      if(form[field] == null || form[field] == undefined) {
        form[field] = 0
      }
    },
    editPackage(scope){
      this.packageForm = JSON.parse(JSON.stringify(scope.row));
      this.packageIndex = scope.$index
      this.openPackage = true;
    },
    /** 保存单位修改信息 */
    submitCompanyForm(){
      Object.assign(this.companyList[0], this.form)
      this.openCompany=false;
    },
    /** 保存套餐修改信息 */
    submitPackageForm(){
      this.$set(this.basepackageList, this.packageIndex, this.packageForm)
      this.openPackage=false;
    },
    /** 提交按钮 */
    submitForm() {
      let form = JSON.parse(JSON.stringify(this.companyInfo))
      if(form.basepackageList){
        form.basepackageList.forEach(item=>{
          item.dimeBaseharmList = this.getHarmLabel(item.dimeBaseharmList)
        })
      }
      if(this.showImport){
        this.importLoading = true
        saveImportCompany(form).then(response => {
          this.$modal.msgSuccess("导入成功");
          this.openImport = false;
          this.getList();
        }).finally(()=>{
          this.importLoading = false;
        });
      }else{
        this.importLoading = true
        editSaveBQCompany(form).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.openImport = false;
          this.getList();
        }).finally(()=>{
          this.importLoading = false;
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const codes = row.companyCode || this.codes;
      this.$modal.confirm('是否确认删除单位编号为"' + codes + '"的数据项？').then(function() {
        return delBasecompany(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleImport(){
      this.SOACode=null
      this.companyList=[]
      this.openImport=true
      this.showImport=true
    }
  }
};
</script>
<style scoped lang="scss">
::v-deep .el-select{
  width: 100%;
}
</style>
